#include "tick.h"

#include "fm33lg0xx.h"

static rt_tick_t volatile rt_tick;

void rt_tick_init() { SysTick_Config(SystemCoreClock / 1000); }

/**
 * This function will notify kernel there is one tick passed. Normally,
 * this function is invoked by clock ISR.
 */
inline static void rt_tick_increase(void)
{
    /* Disable interrupt */
    __disable_irq();

    /* increase the global tick */
    ++rt_tick;

    /* Enable interrupt */
    __enable_irq();
}

extern void CAN_busoff_handle(void);
void        SysTick_Handler()
{
    rt_tick_increase();
    CAN_busoff_handle();
}

/**
 * This function will return current tick from operating system startup
 *
 * @return current tick
 */
rt_tick_t rt_tick_get(void)
{
    /* return the global tick */
    return rt_tick;
}

/**
 * This function will set current tick
 */
void rt_tick_set(rt_tick_t tick) { rt_tick = tick; }

/*
 * This function will return state with tick incream yes or no
 * */
uint8_t rt_tick_incream_judge(void)
{
    rt_tick_t tick_bak = 0;

    if (rt_tick_get() == tick_bak) {
        return 0;
    }

    tick_bak = rt_tick_get();
    return 1;
}
